#ifndef __H_GAME_H__
#define __H_GAME_H__

#include <deque>
#include <vector>
#include <map>
#include <set>
#include "ICC_Timer_Handler.h"
#include "RedisClient.h"
#include "GameBase.h"
#include "DbPart.h"
#include "CClient.h"
#include "def.h"
#include "LogicData.h"
#include "HostBase.h"

class CHost;

const unsigned int g_uid_mod = 100;
const unsigned int g_record_count_mod = 20;
const unsigned int g_flow_cache_num = 200;
const unsigned int g_day_cache_num = 60;
const unsigned int g_cache_dur = 172800;
const unsigned int g_detail_cache_dur = 7200;
const unsigned int g_cache_exp_full_dur = 86400;
const unsigned int g_list_recent_days = 15;

class CGameServer : public BYTimerOject
{
	struct stRecordCache
	{
		std::map<_u64_, stRecordItem> m_lastflow;
		_u64_ m_lastflowtime;
		_u64_ m_lastfetchtime;
		int m_timezoneinmin;
		unsigned int m_count;
		std::map<unsigned int, stRecordDayInfo> m_dayinfo;
		stRecordCache():m_lastflowtime(0), m_lastfetchtime(0), m_timezoneinmin(0),  m_count(0)
		{
		}
		stRecordCache(const stRecordCache& other)
		{
			m_lastflowtime = other.m_lastflowtime;
			m_lastfetchtime = other.m_lastfetchtime;
			m_timezoneinmin = other.m_timezoneinmin;
			m_count = other.m_count;
			m_lastflow = other.m_lastflow;
		}
		stRecordCache(stRecordCache&& other)
		{
			m_lastflowtime = other.m_lastflowtime;
			m_lastfetchtime = other.m_lastfetchtime;
			m_timezoneinmin = other.m_timezoneinmin;
			m_count = other.m_count;
			m_lastflow = std::move(other.m_lastflow);
		}
		stRecordCache& operator = (const stRecordCache& other)
		{
			if (this == &other)
				return *this;

			m_lastflowtime = other.m_lastflowtime;
			m_lastfetchtime = other.m_lastfetchtime;
			m_timezoneinmin = other.m_timezoneinmin;
			m_count = other.m_count;
			m_lastflow = other.m_lastflow;

			return *this;
		}
		stRecordCache& operator = (stRecordCache&& other)
		{
			if (this == &other)
				return *this;

			m_lastflowtime = other.m_lastflowtime;
			m_lastfetchtime = other.m_lastfetchtime;
			m_timezoneinmin = other.m_timezoneinmin;
			m_count = other.m_count;
			m_lastflow = std::move(other.m_lastflow);

			return *this;
		}
	};
public:
	CGameServer();
	virtual ~CGameServer();
public:
	virtual bool InitGame();
	virtual int OnBYTimeOut(int Timerid);
	int ProcessPacket(SocketHandler * pHandler, NETInputPacket* inputPkg);
	int ProcEventGameInit(int ev_id, SpiderEvent* pEvent);
private:
	int ProcGetGameRecord(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcGetGameRecordDetail(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcRecordCachePush(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int fetchRecord(const unsigned int game, const std::string& startym, const unsigned int mseq, const _u64_ startseq, const unsigned int minuseq, const unsigned int uid, const unsigned int reqnum, const _u64_ lasttime, std::map<_u64_, stRecordItem>& record);
	int procTimerCacheCheck(void);
	int procTimerDetailCacheCheck(void);

	void gamerecordCountTable(const unsigned int game, const unsigned int mod, const unsigned int uid, std::string& tablename);
	void monthlistTable(const unsigned int game, std::string& tablename);

	std::map<unsigned int, std::map<unsigned int, stRecordCache> > m_recordcache;
	std::map<unsigned int, std::map<std::string, std::pair<unsigned int, int> > >  m_mindex;
    std::map<unsigned int, std::map<unsigned int, std::pair<std::string, int> > >  m_indexm;

	std::set<std::string> m_gamerecordcounttables;
	std::set<std::string> m_monthlistables;
	std::set<std::string> m_recordtables;
public:
	CHost* m_pHost;
	CDbPart m_dbHandler;
	CDbPart m_dbHandlerAssistant;
	CDbPart m_dbHandlerMaintenance;
};


#endif


